package com.zdkj.subway.st.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdkj.common.core.exceltools.IExcelExport;
import com.zdkj.common.core.util.R;
import com.zdkj.common.core.util.SpringContextHolder;
import com.zdkj.common.log.annotation.SysLog;
import com.zdkj.common.security.service.PigxUser;
import com.zdkj.common.security.util.SecurityUtils;
import com.zdkj.subway.st.entity.SwStTicketDestroy;
import com.zdkj.subway.st.service.impl.SwStTicketDestroyServiceImpl;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @ProjectName: zdkj
 * @Package: com.zdkj.subway.st.controller
 * @ClassName: SwStTicketReceiveController
 * @Description:
 * @Author: kangjian
 * @CreateDate: 2020/1/8 14:48
 * @Version: 1.0
 */
@RestController
@AllArgsConstructor
@RequestMapping("/swStTicketDestroy")
@Api(value = "swStTicketDestroy", tags = "车票销毁记录")
public class SwStTicketDestroyController {

    private SwStTicketDestroyServiceImpl swStTicketDestroyService;

    @GetMapping("/{id}")
    public R getById(@PathVariable String id) {
        return R.ok(swStTicketDestroyService.getById(id));
    }

    @GetMapping("/page")
    public R<IPage> getDictPage(Page page, SwStTicketDestroy swStTicketDestroy) {
        //出库时间
        List<String> dateCondition = swStTicketDestroy.getDates1();
        QueryWrapper<SwStTicketDestroy> lambdaQueryWrapper = Wrappers.query(swStTicketDestroy);
        lambdaQueryWrapper.orderByDesc("out_storage_time");
        //LambdaQueryWrapper<SwStTicketDestroy> lambdaQueryWrapper = Wrappers.lambdaQuery(swStTicketDestroy);
        if (ObjectUtil.isNotEmpty(dateCondition)) {
            lambdaQueryWrapper.lambda().between(SwStTicketDestroy::getOutStorageTime, dateCondition.get(0), dateCondition.get(1));
        }
        String destoryTime = swStTicketDestroy.getDestroyTimeDTO();
        //String outStorageTime = swStTicketDestroy.getOutStorageTimeDTO();
        if (StringUtils.isNotBlank(destoryTime)) {
            lambdaQueryWrapper.lambda().likeRight(SwStTicketDestroy::getDestroyTime, destoryTime);
        }
        /*if (StringUtils.isNotBlank(outStorageTime)) {
            lambdaQueryWrapper.lambda().likeRight(SwStTicketDestroy::getOutStorageTime, outStorageTime);
        }*/
        return R.ok(swStTicketDestroyService.page(page, lambdaQueryWrapper));
    }

    @SysLog("新增-车票销毁记录")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('st_ticket_destroy_add')")
    public R save(@Valid @RequestBody SwStTicketDestroy swStTicketDestroy) {
        return R.ok(swStTicketDestroyService.save(swStTicketDestroy));
    }

    @SysLog("删除-车票销毁记录")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('st_ticket_destroy_delete')")
    public R removeById(@PathVariable String id) {
        return R.ok(swStTicketDestroyService.removeById(id));
    }

    @PutMapping
    @SysLog("修改-车票销毁记录")
    @PreAuthorize("@pms.hasPermission('st_ticket_destroy_update')")
    public R updateById(@Valid @RequestBody SwStTicketDestroy swStTicketDestroy) {
        return R.ok(swStTicketDestroyService.updateById(swStTicketDestroy));
    }

    @PutMapping("/destroy")
    @SysLog("领用-车票销毁记录")
    public R destroyTicket(@Valid @RequestBody SwStTicketDestroy destroy) {
        SwStTicketDestroy swStTicketDestroy = swStTicketDestroyService.getById(destroy.getId());
        PigxUser user = SecurityUtils.getUser();
        swStTicketDestroy.setCreatorName(user.getUsername());
        swStTicketDestroy.setCreatorId(user.getId());
        swStTicketDestroy.setIsReceive("是");
        swStTicketDestroy.setDestroyer(destroy.getDestroyer());
        swStTicketDestroy.setDestroyerId(destroy.getDestroyerId());
        swStTicketDestroy.setDestroyTime(destroy.getDestroyTime());
        swStTicketDestroy.setRemark(destroy.getRemark());
        return R.ok(swStTicketDestroyService.updateById(swStTicketDestroy));
    }

    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('st_ticket_destroy_export')")
    public void exportExcel(SwStTicketDestroy swStTicketDestroy, HttpServletResponse response) {
        List<SwStTicketDestroy> swStTicketDestroylist = swStTicketDestroyService.list(Wrappers.query(swStTicketDestroy));
        IExcelExport excelExport = SpringContextHolder.getBean("commonExcelExport");//设置头信息
        Workbook workbook = excelExport.parse(swStTicketDestroylist);
        try {
            workbook.write(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}